<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
      ul {
        list-style: none;
        padding: 0;
        margin: 0;
      }

      .header {
        display: flex;
        height: 40px;
      }

      .header li {
        flex: 1;
        height: 40px;
        text-align: center;
        line-height: 40px;
        font-size: 14px;
        font-weight: bold;
        border: 1px solid #ddd;
      }

      .content-item {
        display: flex;
      }

      .content-item li {
        flex: 1;
        height: 30px;
        text-align: center;
        line-height: 30px;
        border: 1px solid #ddd;
      }

      .footer {
        width: 100%;
        height: 40px;
        text-align: center;
        line-height: 40px;
        border: 1px solid #ddd;
      }
    </style>
</head>
<body>
    <div id="app">
      <ul class="header">
        <li v-for="item in headerArr">{{ item }}</li>
      </ul>

      <ul class="content-item" v-for="item in tableData">
        <li>{{item.code}}</li>
        <li>{{item.name}}</li>
        <li>{{item.price}}</li>
        <li>
          <button v-on:click="add(item)">+</button>
          <span>{{item.count}}</span>
          <button v-on:click="remove(item)">-</button>
        </li>
        <li>{{ item.total ? item.total : (item.count * item.price).toFixed(2) }}</li>
      </ul>

      <div class="footer">
        总价：￥
        {{ tableTotal }}
      </div>
    </div>

    <script src="js/vue.js"></script>

    <script>
        let myApp = new Vue({
            el: '#app',
            data: {
                headerArr: ['编号','名称','单价','数量','总价'],
                tableData: [
                    {code: 1, name: 'css', price: 56.2, count: 1, total: 0},
                    {code: 2, name: 'javascript', price: 45.1, count: 1, total: 0},
                    {code: 3, name: 'HTML', price: 19.9, count: 1, total: 0}
                ]
            },
            methods: {
              add (data) {
                console.log(data)
                data.count++

                data.total = (data.count * data.price).toFixed(2)
              },
              remove (data) {
                if (data.count) {
                  data.count--

                  data.total = (data.count * data.price).toFixed(2)
                }
              }
            },
            computed: {
              tableTotal () {
                var total = this.tableData.reduce((prev, next) => {
                  console.log('prev', prev)
                  console.log('next', next)
                  return prev + (next.price * next.count)
                }, 0)

                return total.toFixed(2)
              }
            }
        })
    </script>
</body>
</html>